<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 2022/4/15
 * Time: 10:45
 * 有来音频
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');

class Youlai_voice extends CI_Controller
{
    /**
     * 获取科室对应疾病id
     */
    public function ids()
    {
        $url = "https://www.youlai.cn/ask/voicelist/1_0.html";
        $content = geturl($url);
        $html = str_get_html($content);
        foreach ($html->find('dd', 2)->find('a') as $ak => $a) {
            $dept1 = $a->href;
            $dept1_id = str_replace(['/ask/voicelist/', '.html'], '', $dept1);
            $dept1_name = trim($a->innertext);

            $url = "https://www.youlai.cn/ask/voicelist/" . $dept1_id . ".html";
            $content = geturl($url);
            $html = str_get_html($content);

            foreach ($html->find('dd', 3)->find('a') as $ak2 => $a2) {
                if($ak2 == 0) {
                    continue;
                }
                $dept2 = $a2->href;
                $dept2_id = str_replace(['/ask/voicelist/'.$dept1_id.'_', '.html'], '', $dept2);
                $dept2_name = trim($a2->innertext);


                $sql = "";
                $url = "https://www.youlai.cn/ask/voicelist/" . $dept1_id . "_" . $dept2_id . ".html";
                $content = geturl($url);
                $html = str_get_html($content);
                echo $url."<br>";
                // 通过科室id获取疾病列表
                if(count($html->find('dd', 4)->find('a')) <= 1){
                    continue;
                }
                foreach ($html->find('dd', 4)->find('a') as $aak => $aa) {
                    if ($aak == 0) {
                        continue;
                    }
                    $dis = $aa->href;
                    $dis_id = str_replace(['/ask/voicelist/'.$dept1_id.'_'.$dept2_id.'_', '.html'], '', $dis);
                    $dis_name = trim($aa->innertext);
                    $sql .= "('" . $dept1_id . "','" . $dept2_id .  "','" . $dept1_name .  "','" . $dept2_name . "'," . $dis_id . ",'" . $dis_name . "'),";
                }
                $sql = substr($sql, 0, -1);
                $sql = 'INSERT ignore INTO ai_youlai_voice_ids(dept1_id,dept2_id,dept1_name,dept2_name,dis_id,dis_name) VALUES ' . $sql;
                //echo $sql;exit;
                $this->db->query($sql);

            }

        }


        //dd($div);
    }

    /**
     * 获取详情列表
     */
    public function urls(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $did = $redis->lpop('youlai_list');
        //$did = 7;
        if(!$did){
            echo "nodata";
            exit;
        }
        $ids = $this->db->where('id',$did)->get('youlai_voice_ids')->row_array();
        if(!$ids){
            echo "nodata";
            exit;
        }
        for($i=1;$i<=10;$i++){ // 最多十页
            //https://www.youlai.cn/ask/voicelist/1_12_1_0_2.html
            $list_html = "https://www.youlai.cn/ask/voicelist/".$ids['dept1_id']."_".$ids['dept2_id']."_".$ids['dis_id']."_0_".$i.".html";
            //echo $list_html."<br>";
            $content = geturl($list_html);
            $html = str_get_html($content);
            $lis = $html->find("ul[class=cur05] li");
            $sql = "";
            foreach($lis as $li){
                $did = $ids['id'];
                $dept1 = $ids['dept1_name'];
                $dept2 = $ids['dept2_name'];
                $dis = $ids['dis_name'];
                $title = trim($li->find("a",0)->find("h3",0)->innertext);
                $url = trim($li->find("a",0)->href);
                $sql .= "(".$did.",'".$dept1."','".$dept2."','".$dis."','".$title."','".$url."'),";
            }
            $sql = substr($sql,0,-1);
            $sql = 'INSERT ignore INTO ai_youlai_voice_url(did,dept1,dept2,dis,title,url) VALUES '.$sql;
            //echo $sql;exit;
            $this->db->query($sql);
            if(count($lis) < 20){ // 不够20的话，肯定没有下一页了
                break;
            }
        }
        echo $did + 1;
    }

    public function info(){

    }

    public function url_push(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        $ids = $this->db->query("select id from ai_youlai_voice_url")->result_array();
        foreach($ids as $id){
            $redis->rPush('youlai_list_video',$id['id']);
        }

    }
}